package com.itheima.web;

import com.itheima.bean.ShoppingCart;
import com.itheima.common.R;
import com.itheima.config.BaseContext;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R list(){
        List<ShoppingCart> shoppingCartList = shoppingCartService.findByUid(BaseContext.getThreadLocal());
        return R.success(shoppingCartList);
    }
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart shoppingCart1 = shoppingCartService.add(shoppingCart);
        if (shoppingCart1!=null){
            return R.success(shoppingCart1);
        }
        return R.error("添加失败！");
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart sub = shoppingCartService.sub(shoppingCart);
        if (sub!=null){
            return R.success(sub);
        }
        return R.success("修改成功！");
    }
}
